﻿using System;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Xml.Linq;

namespace SilverAmp
{

    public class IsolatedStorageSongInformationProvider: ISongInformationProvider
    {
        private byte[] buffer;

        public event EventHandler<TrackEventArgs> SongDetailsUpdateComplete;

        public void InvokeSongDetailsUpdateComplete(ITrackInfo e)
        {
            EventHandler<TrackEventArgs> handler = SongDetailsUpdateComplete;
            if (handler != null) handler(this, new TrackEventArgs() { TrackInfo = e });
        }

        public void UpdateSongInformation(ITrackInfo song)
        {
            var storage =IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("SongInformation.xml"))
            {
                IsolatedStorageFileStream file = 
                    storage.OpenFile("/SongInformation.xml", FileMode.Open, FileAccess.Read);
                var xml = XDocument.Load(file);
                             
            }
            else
            {
                InvokeSongDetailsUpdateComplete(song);
            }
        }

        public void SaveSongInformation(ITrackInfo song)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream file = 
                storage.OpenFile("/SongInformation.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            var xml = XDocument.Load(file);
        }
    }

    public class ID3SongInformationProvider : ISongInformationProvider
    {
        private BackgroundWorker bw = new BackgroundWorker();

        public event EventHandler<TrackEventArgs> SongDetailsUpdateComplete;

        public void UpdateSongInformation(ITrackInfo song)
        {
           // _getSongInfoCallback = new GetSongInfoCallback(GetSongInfo);

            //Deployment.Current.Dispatcher.BeginInvoke(_getSongInfoCallback, song);
            //bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            //bw.RunWorkerAsync(song);
            //bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            InvokeSongDetailsUpdateComplete(e.Result as TrackInfo);
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            var song = e.Argument as ITrackInfo;
            TrackInfo trackInfo = GetTrackInfo(song.Fileinfo);
            e.Result = trackInfo;
        }

        private void GetSongInfo(TrackInfo song)
        {
            TrackInfo trackInfo = GetTrackInfo(song.Fileinfo);
            InvokeSongDetailsUpdateComplete(trackInfo);
        }

        public TrackInfo GetTrackInfo(FileInfo fileInfo)
        {
            MediaFile file = fileInfo;
            var tags = TagLib.File.Create(file);
            return new TrackInfo
                       {
                           Name = tags.Tag.Title,
                           Artist = tags.Tag.AlbumArtists.Length > 0 ? tags.Tag.AlbumArtists[0] : "",
                           Tags = tags.Tag,
                           Fileinfo = fileInfo
                       };
        }


        private delegate void GetSongInfoCallback(TrackInfo songs);
        private GetSongInfoCallback _getSongInfoCallback;

        private void InvokeSongDetailsUpdateComplete(TrackInfo e)
        {
            EventHandler<TrackEventArgs> handler = SongDetailsUpdateComplete;
            if (handler != null) handler(this, new TrackEventArgs() { TrackInfo = e });
        }
    }
}